package com.cs.controller;

import cn.hutool.core.lang.Snowflake;
import com.cs.domain.*;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.cs.domain.CSNotice;
import com.cs.domain.vo.*;
import com.cs.service.ICsTenderService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.annotation.RequiresLogin;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.annotation.RequiresRoles;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.cs.util.CSRuleCreate.createCode;

/**
 * 招标项目Controller
 *
 * @author ruoyi
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/tender")
public class CsTenderController extends BaseController {
    @Autowired
    private ICsTenderService csTenderService;


    /**
     * 查询招标项目列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CsTender csTender) {
        startPage();
        List<CsTender> list = csTenderService.selectCsTenderList(csTender);
        return getDataTable(list);
    }

    @GetMapping("/getTenderProjectInfo/{iNumber}")
    public Map<String, Object> getTenderProjectInfo(@PathVariable String iNumber) {
        return csTenderService.getTenderProjectInfo(iNumber);
    }

    /**
     * 导出招标项目列表
     */
    @RequiresPermissions("system:tender:export")
    @Log(title = "招标项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsTender csTender) {
        List<CsTender> list = csTenderService.selectCsTenderList(csTender);
        ExcelUtil<CsTender> util = new ExcelUtil<CsTender>(CsTender.class);
        util.exportExcel(response, list, "招标项目数据");
    }

    /**
     * 获取招标项目详细信息
     */
    @RequiresPermissions("system:tender:query")
    @GetMapping(value = "/{sId}")
    public AjaxResult getInfo(@PathVariable("sId") Long sId) {
        return success(csTenderService.selectCsTenderBySId(sId));
    }

    /**
     * 新增招标项目
     */
    @Log(title = "招标项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TenderInviteSupplierVO csTender) {
        return toAjax(csTenderService.insertCsTender(csTender));
    }

    /**
     * 修改招标项目
     */
    @RequiresPermissions("system:tender:edit")
    @Log(title = "招标项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsTender csTender) {
        return toAjax(csTenderService.updateCsTender(csTender));
    }


    /**
     * 删除招标项目
     */
    @RequiresPermissions("system:tender:remove")
    @Log(title = "招标项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sIds}")
    public AjaxResult remove(@PathVariable Long[] sIds) {
        return toAjax(csTenderService.deleteCsTenderBySIds(sIds));
    }

    /**
     * 更新招标项目
     */
    @RequiresLogin
    @RequiresRoles(value = {"admin"})
    @PutMapping("/updateTenderProject")
    public R<Integer> updateTenderProject(@RequestBody TenderFileVO csTender) {
        return csTenderService.updateTenderProject(csTender);
    }

    /**
     * 删除招标项目
     */
    @RequiresLogin
    @RequiresRoles(value = {"admin"})
    @DeleteMapping("/deleteTenderProject/{iNumber}")
    public R<Integer> deleteTenderProject(@PathVariable String iNumber) {
        return csTenderService.deleteTenderProject(iNumber);
    }

    /**
     * 将立标状态改成招标状态(顺便更新)
     */
    @PutMapping("/changeTenderProjectStatus")
    @RequiresRoles(value = {"admin"})
    @RequiresLogin
    public R<Integer> changeTenderProjectStatus(@RequestBody TenderFileVO csTender) {
        return csTenderService.changeTenderProjectStatus(csTender);
    }


    /**
     * 招标公告
     */
    @GetMapping("/getTenderNoticeByINumber/{iNumber}")
    public List<CSNotice> getTenderNoticeByINumber(@PathVariable String iNumber) {
        return csTenderService.getTenderNoticeByINumber(iNumber);
    }


    /**
     * 招标公告新增
     */
    @PostMapping("/addTenderNotice")
    @RequiresRoles(value = {"common","admin"},logical = Logical.OR)
    public R<Integer> addTenderNotice(@RequestBody NoticeVO noticeVO) {
        return csTenderService.addTenderNotice(noticeVO);
    }

    /**
     * 招标公告详细信息
     */
    @GetMapping("/getNoticeInfoByUId/{uId}")
    public NoticeVO getNoticeInfoByUId(@PathVariable String uId) {
        return csTenderService.getNoticeInfoByUId(uId);
    }

    /**
     * 提交招标公告
     */
    @PutMapping("/submitTenderNotice")
    @RequiresLogin
    public R<Integer> submitTenderNotice(@RequestBody NoticeVO noticeVO) {
        return csTenderService.submitTenderNotice(noticeVO);
    }


    /**
     * 编辑招标公告
     */
    @PutMapping("/editTenderNotice")
    @RequiresLogin
    public R<Integer> editTenderNotice(@RequestBody NoticeVO noticeVO) {
        return csTenderService.editTenderNotice(noticeVO);
    }

    /**
     * 审核招标公告
     */
    @PutMapping("/examineNoticeRow")
    @RequiresRoles(value = {"admin"})
    public R<Integer> examineNoticeRow(@RequestBody NoticeVO noticeVO) {
        return csTenderService.examineNoticeRow(noticeVO);
    }


    /**
     * 发布招标公告
     */
    @PutMapping("/releaseNoticeRow/{uId}")
    @RequiresRoles(value = {"admin"})
    public R<Integer> releaseNoticeRow(@PathVariable String uId) {
        return csTenderService.releaseNoticeRow(uId);
    }


    /**
     * 删除招标公告
     */
    @DeleteMapping("/deleteNoticeRow/{uId}")
    @RequiresRoles(value = {"admin"})
    public R<Integer> deleteNoticeRow(@PathVariable String uId) {
        return csTenderService.deleteNoticeRow(uId);
    }

    /**
     * 招标项目文件集合
     */
    @GetMapping("/getFileListByINumber/{iNumber}")
    public List<File> getFileListByINumber(@PathVariable String iNumber) {
        return csTenderService.getFileListByINumber(iNumber);
    }


    /**
     * 驳回公告
     */
    @PutMapping("/rejectNotice/{uId}")
    @RequiresRoles(value = {"admin"})
    public R<Integer> rejectNotice(@PathVariable Integer uId) {
        return csTenderService.rejectNotice(uId);
    }


    /**
     * 添加招标文件
     */
    @PostMapping("/addTenderFile")
    @RequiresRoles(value = {"admin"})
    public R<Integer> addTenderFile(@RequestBody TenderFileNameVO fileNameVO) {
        return csTenderService.addTenderFile(fileNameVO);
    }


    /**
     * 删除招标文件
     */
    @DeleteMapping("/deleteTenderFile/{bDocuId}")
    @RequiresRoles(value = {"admin"})
    public R<Integer> deleteTenderFile(@PathVariable Integer bDocuId) {
        return csTenderService.deleteTenderFile(bDocuId);
    }

    /**
     * 投标 页面的 获取标书
     *
     * @param iNumber 项目编号
     * @return 返回项目信息, 投标人信息
     */
    @GetMapping("/bidGetDocumentPeople/{iNumber}")
    public R<Map<String, Object>> bidGetDocumentPeople(@PathVariable String iNumber) {
        return csTenderService.bidGetDocumentPeople(iNumber);
    }


    /**
     * 投标 页面的 投递标书
     *
     * @param iNumber 项目编号
     * @return 返回项目信息, 投标人信息
     */
    @GetMapping("/getBidPeopleList/{iNumber}")
    public R<List<SubmissionSupplierVO>> getBidPeopleList(@PathVariable String iNumber) {
        return csTenderService.getBidPeopleList(iNumber);
    }


    /**
     * 投标中 获得项目信息
     *
     * @param iNumber
     * @return
     */
    @GetMapping("/getTender/{iNumber}")
    public R<CsTender> getTender(@PathVariable String iNumber) {
        return csTenderService.getTender(iNumber);
    }

    /**
     * 开标是否开始
     *
     * @param iNumber
     * @return
     */
    @GetMapping("/getProjectStateAndStartTime/{iNumber}")
    public R<TenderNoticeVO> getProjectStateAndStartTime(@PathVariable String iNumber) {
        return csTenderService.getProjectStateAndStartTime(iNumber);
    }

    /**
     * 获得所有投标商(写反了,这个实际是中标候选人)
     *
     * @param tender 任意一个包含iNumber的对象都可以
     * @return 分页
     */
    @GetMapping("/getAllSupplierList")
    public TableDataInfo getAllSupplierList(CsTender tender) {
        startPage();
        return getDataTable(csTenderService.getAllSupplierList(tender));
    }

    /**
     * 获得所有投标商
     * @param iNumber
     * @return
     */
    @GetMapping("/getAllBidder/{iNumber}")
    public List<CsSupplier> getAllBidder(@PathVariable String iNumber) {
      List<CsSupplier> suppliers= csTenderService.getAllBidder(iNumber);
      return suppliers;
    }

    /**
     * 添加中标候选人
     */
    @PostMapping("/addCandidate")
    @RequiresRoles(value = {"admin"})
    public R<Integer> addCandidate(@RequestBody CsCandidate csCandidate) {
        return R.ok(csTenderService.addCandidate(csCandidate));
    }


    /**
     * 获得标书(新增)
     */
    @PostMapping("/getBidDocument/{iNumber}")
    @RequiresRoles(value = {"supplier"})
    public R<List<File>> getBidDocument(@PathVariable String iNumber) {
        return R.ok(csTenderService.getBidDocument(iNumber));
    }

    /**
     * 投递标书(新增)
     */
    @PostMapping("/addSubmission")
    @RequiresRoles(value = {"supplier"})
    public R<Integer> addSubmission(@RequestBody CSSubmission csSubmission) {
        return R.ok(csTenderService.addSubmission(csSubmission));
    }

    /**
     * 获得中标候选人(公示版)
     * @param iNumber
     * @return
     */
    @GetMapping("/getBidWinningCandidate/{iNumber}")
    public R<Map<String,Object>> getBidWinningCandidate(@PathVariable String iNumber){
        return R.ok(csTenderService.getBidWinningCandidate(iNumber));
    }

    /**
     * 设置中标候选人公示时间(或者是中标公示时间)
     */
    @PutMapping("/setStartTenderTime")
    @RequiresRoles(value = {"admin"})
    public R<Integer> setStartTenderTime(@RequestBody CsTender csTender){
        return R.ok(csTenderService.setStartTenderTime(csTender));
    }


    /**
     * 确认中标人
     */
    @PutMapping("/confirmWinningBidder")
    @RequiresRoles(value = {"admin"})
    public R<Integer> confirmWinningBidder(@RequestBody List<CsCandidate> strings){
        return R.ok(csTenderService.confirmWinningBidder(strings));
    }

    /**
     * 获得发送通知的供应商
     */
    @GetMapping("/getBidWinningCandidateAndSended/{iNumber}")
    public R<Map<String, Object>> getBidWinningCandidateAndSended(@PathVariable String iNumber){
        System.out.println(iNumber+"ssss");
        return R.ok(csTenderService.getBidWinningCandidateAndSended(iNumber));
    }

    /**
     * 发送通知给供应商
     */
    @PostMapping("/setNoticeToSupplier")
    @RequiresRoles(value = {"admin"})
    public R<List<WinningResultsSupplier>> setNoticeToSupplier(@RequestBody List<CsWinningResults> winningResults){
        return R.ok(csTenderService.setNoticeToSupplier(winningResults));
    }


    /**
     * 查询招标项目数量(不知道是否有用此接口)
     */
    @PostMapping("/selectTenderCount")
    public R selectTenderCount() {
        return R.ok(csTenderService.selectTenderCount());
    }


    /**
     * 查询招标阶段的项目
     *
     * @param csTender
     * @return
     */
    @PostMapping("/selectTenderByState")
    @RequiresLogin
    public List<CsTender> selectTenderByState(@RequestBody CsTender csTender) {
        return csTenderService.selectTenderByState(csTender);
    }

    @PostMapping(value = "/selectTenderByStateCount")
    @RequiresLogin
    public R selectTenderByStateCount(@RequestBody CsTender csTender) {
        return R.ok(csTenderService.selectTenderByStateCount(csTender));
    }


    /**
     * 专门用于获得项目状态和所有时间的方法
     */
    @GetMapping(value = "/getTenderStatusTime/{iNumber}")
    public R<TenderStatusTime> getTenderStatusTime(@PathVariable String iNumber) {
        return R.ok(csTenderService.getTenderStatusTime(iNumber));
    }

    /**
     * 供应商获得中标结果的
     */
    @GetMapping(value = "/getBidingResult")
    public R<List<CsWinningResults>> getBidingResult() {
        return R.ok(csTenderService.getBidingResult());
    }

    /**
     * 供应商获得未中标结果的
     */
    @GetMapping(value = "/getNoBidingResult")
    public R<List<CsCandidate>> getNoBidingResult() {
        return R.ok(csTenderService.getNoBidingResult());
    }


    /**
     * 供应商获得个人信息
     */
    @GetMapping(value = "/getPersonInfo")
    public R<CsSupplier> getPersonInfo() {
        return R.ok(csTenderService.getPersonInfo());
    }

    @GetMapping("/getLoginTenderInfo")
    public R<Map<String,Object>> getLoginTenderInfo(){
        return R.ok(csTenderService.getLoginTenderInfo());
    }

    @GetMapping("/getTenderNoticeInfo/{iNumber}")
    public R<TenderNoticeVO> getTenderNoticeInfo(@PathVariable String iNumber){
        return R.ok(csTenderService.getTenderNoticeInfo(iNumber));
    }
    /**
     * 获得中标候选人
     */
//    @GetMapping("/getWinnResult/{iNumber}")
//    public R<List<CsWinningResults>> getWinnResult(@PathVariable String iNumber){
//        return R.ok(csTenderService.getWinnResult(iNumber));
//    }

    /**
     * 获得中标候选人
     */
    @GetMapping("/getWinnResult/{iNumber}")
    public R<List<CsCandidate>> getWinnResult(@PathVariable String iNumber){
        return R.ok(csTenderService.getWinnResult(iNumber));
    }

    /**
     * 获得分类
     */
    @GetMapping("/getCategory")
    public R<List<CSCategory>> getCategory(){
        return R.ok(csTenderService.getCategory());
    }

}
